using System;
using System.Collections.Generic;
#if (DEBUG)
using System.Diagnostics;
#endif // #if (DEBUG)
using System.Text;
using System.Windows.Controls;
using System.Reflection;
using System.ComponentModel;

namespace FRL.DebtChased.Silverlight.Controls
{

	/// <summary>
	/// Declaration of ComboBoxObject class
	/// </summary>
	public class ComboBoxObject : Object
	{

		/////////////////////////////////////////////////////////////
		// attributes

		private int m_iItemData = 0;
		public int ItemData
		{
			get { return m_iItemData; }
		}

		private Object m_oTag = null;
		public Object Tag
		{
			get { return m_oTag; }
		}

		private String m_strText = "";
		public String Text
		{
			get { return m_strText; }
			set { m_strText = value; }
		}

		/////////////////////////////////////////////////////////////
		// construction

		public ComboBoxObject(String strText, int iItemData,Object oTag)
		{

			// initialize member variables
			m_iItemData = iItemData;
			m_strText = strText;
			m_oTag = oTag;

		}

		/////////////////////////////////////////////////////////////
		// operations

		static public void SelectByItemData( ComboBox ctrl ,int iItemData)
		{

			// loop through combo-box
			foreach( ComboBoxObject item in ctrl.Items )
			{

				// compare item-data
				if(item.ItemData==iItemData)
				{

					// select
					ctrl.SelectedItem = item;

					// break out of loop
					break;

				}

			}

			// if no item selected...
			if(ctrl.SelectedItem==null)
			{
#if (DEBUG)
				Debug.Assert(false);
#endif // #if (DEBUG)

				// select top item
				ctrl.SelectedIndex = 0;

			}

		}

		static public void SelectByItemData(ListBox ctrl, int iItemData)
		{

			// loop through combo-box
			foreach (ComboBoxObject item in ctrl.Items)
			{

				// compare item-data
				if (item.ItemData == iItemData)
				{

					// select
					ctrl.SelectedItem = item;

					// break out of loop
					break;

				}

			}

			// if no item selected...
			if (ctrl.SelectedItem == null)
			{
#if (DEBUG)
				Debug.Assert(false);
#endif // #if (DEBUG)

				// select top item
				ctrl.SelectedIndex = 0;

			}

		}

		static public void SelectByTag(ComboBox ctrl, Object oTag)
		{

			// loop through combo-box
			foreach (ComboBoxObject item in ctrl.Items)
			{

				// compare item-data2
				if (item.Tag == oTag)
				{

					// select
					ctrl.SelectedItem = item;

					// break out of loop
					break;

				}

			}

			// if no item selected...
			if (ctrl.SelectedItem == null)
			{
#if (DEBUG)
				Debug.Assert(false);
#endif // #if (DEBUG)

				// select top item
				ctrl.SelectedIndex = 0;

			}

		}

		static public void SelectByTagString(ComboBox ctrl, string strTag)
		{

			// loop through combo-box
			foreach (ComboBoxObject item in ctrl.Items)
			{

				// compare item-data2
				if (
					( (strTag == null) && (item.Tag==null) )
					||
					(
						(item.Tag != null)
						&&
						(string.Compare(item.Tag.ToString(), strTag, StringComparison.CurrentCultureIgnoreCase) == 0)
					)
				)
				{

					// select
					ctrl.SelectedItem = item;

					// break out of loop
					break;

				}

			}

			// if no item selected...
			if (ctrl.SelectedItem == null)
			{
#if (DEBUG)
				Debug.Assert(false);
#endif // #if (DEBUG)

				// select top item
				ctrl.SelectedIndex = 0;

			}

		}

		public static void LoadFromDictionary(ComboBox ctrl, Dictionary<int, string> dict)
		{

			// Loop through dictionary
			foreach (var di in dict)
				ctrl.Items.Add( new ComboBoxObject(di.Value,di.Key,null));

		}

		public static void LoadFromEnumeration( ComboBox ctrl, Type enumType, int iIgnore=-1 )
		{

			// Get the type fields
			FieldInfo[] fi = enumType.GetFields();
			for (int i1 = 0; i1 < fi.Length; i1++)
			{

				// Ifnore non-literals
				if ((fi[i1].Attributes & FieldAttributes.Literal) == 0)
					continue;

				// Get the name
				string strName = fi[i1].Name;
				DescriptionAttribute attr = Attribute.GetCustomAttribute( fi[i1], typeof(DescriptionAttribute) ) as DescriptionAttribute;
				if (attr != null)
					strName = attr.Description;

				// Get the value
				int iValue = (int)fi[i1].GetValue(enumType);

				// Some values are to be ignored...
				if (iValue == iIgnore)
					continue;

				// Construct a new item
				ComboBoxObject cbo = new ComboBoxObject(strName, iValue, null);

				// Add to list
				ctrl.Items.Add(cbo);

			}

		}

		/////////////////////////////////////////////////////////////
		// overrides

		public override string ToString()
		{
			return m_strText;
		}

	}

}
